package com.hmf.config;

import com.hmf.intercept.ProjectInterceptor;
import com.hmf.intercept.UrlAccessFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

//@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private ProjectInterceptor projectInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置多拦截器
        registry.addInterceptor(projectInterceptor).addPathPatterns("/", "/*");


    }

    /**
     * /**
     * * 消息内容转换配置
     *
     * @param converters
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
//        List<MediaType> list = new ArrayList<MediaType>();
//        list.add(MediaType.APPLICATION_JSON); // 支持JSON类型
//        list.add(MediaType.TEXT_PLAIN); // 支持文本类型，如果需要的话
//        //MappingJackson2HttpMessageConverter来处理JSON类型的数据
//        MappingJackson2HttpMessageConverter converter2 = new MappingJackson2HttpMessageConverter();
//        converter2.setSupportedMediaTypes(list);
//        converter2.setDefaultCharset(Charset.defaultCharset());
//        converters.add(converter2);
//    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //字符串
        StringHttpMessageConverter converter = new StringHttpMessageConverter();

        converter.setDefaultCharset(Charset.defaultCharset());
        converters.add(converter);


        //MappingJackson2HttpMessageConverter来处理JSON类型的数据
        MappingJackson2HttpMessageConverter converter2 = new MappingJackson2HttpMessageConverter();

//        converter2.setDefaultCharset(Charset.defaultCharset());
        converters.add(converter2);
    }

    /**
     * 解决跨域问题
     **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                //.allowedMethods("POST", "GET")
                .allowedMethods("POST", "GET","DELETE","PUT")
                .allowedOrigins("http://localhost:8080") // 设置允许的前端域名和端口
                .allowedOrigins("http://localhost:8082") // 设置允许的前端域名和端口
                .allowedOrigins("*");
    }



    /**
     * 配置静态访问资源
     *
     * @Override
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toLogin").setViewName("login");
    }
}